package cn.kgc.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.LogRecord;

public class loginFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化登录认证过滤器");

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入了登录认证过滤器");
        HttpSession session =((HttpServletRequest)servletRequest).getSession();

        String user = (String) session.getAttribute("userName");
        if(user==null){
            System.out.println("退出了登录认证过滤器");
            ((HttpServletResponse) servletResponse).sendRedirect("/web01/day02/login.jsp");
        }else{
            doFilter(servletRequest,servletResponse,filterChain);
            System.out.println("退出了登录认证过滤器");


        }
    }

    @Override
    public void destroy() {
        System.out.println("销毁了登录认证过滤器");

    }
}
